import config from '@/config'
// #ifdef H5
import manifest from '@/manifest.json'
// #endif

import { appUrl } from './baseUrl.js'

// 对车辆各状态进行转文字
export const getStateText = (state) => {
	if(state == 1) {
		return '离线-从未报定位'
	}else if(state == 2) {
		return '离线-欠费'
	}else if(state == 3) {
		return '离线'
	}else if(state == 4) {
		return '离线-报警'
	}else if(state == 5) {
		return '在线-从未报定位'
	}else if(state == 6) {
		return '在线-欠费'
	}else if(state == 7) {
		return '在线-行驶'
	}else if(state == 8) {
		return '在线-行驶-报警'
	}else if(state == 9) {
		return '在线-停车-ACC关'
	}else if(state == 10) {
		return '在线-停车-ACC开'
	}else if(state == 11) {
		return '在线-停车-ACC关-报警'
	}else if(state == 12) {
		return '在线-停车-ACC开-报警'
	}else {
		return '在线-无效定位'
	}
}

// 对实时视频的状态进行文字返回
export const getVideoText = (state) => {
	if(state == -6) {
		return '车辆欠费'
	}else if(state == -4) {
		return '设备没有回应，请稍后再试'
	}else if(state == -3) {
		return '车辆不存在'
	}else if(state == -2) {
		return '系统异常'
	}else if(state == -1) {
		return '设备离线'
	}else if(state == 1) {
		return '设备返回失败'
	}else if(state == 2) {
		return '设备返回消息有误'
	}else if(state == 3) {
		return '设备不支持'
	}else if(state == 13) {
		return '对讲被占用'
	}else if(state == 14) {
		return '广播被占用'
	}else if(state == 15) {
		return '上级平台正在播放'
	}else if(state == 16) {
		return '紧急报警正在录像'
	}else if(state == 17) {
		return '广播端口分配完毕'
	}else if(state == 20) {
		return '该通道正在回放'
	}else if(state == 21) {
		return '上级平台正在回放'
	}else if(state == 30) {
		return '809成功'
	}else if(state == 31) {
		return '809返回失败'
	}else if(state == 32) {
		return '809不支持'
	}else if(state == 33) {
		return '809会话结束'
	}else if(state == 34) {
		return '809时效口令错误'
	}else if(state == 35) {
		return '809不满足跨域条件'
	}else if(state == 44) {
		return '服务器带宽已满，请稍后再试'
	}else {
		return '未知错误'
	}
}

// 返回图片链接
export const getImgUrl = (state) => {
	const { cmpId } = uni.getStorageSync('systemInfo')
	return `${appUrl}/${uni.getStorageSync('serverIpInfo')}/gps-web/login?img=${state}&cmpId=${cmpId}`
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}
// #ifdef H5
/**
 * 为path添加重定向url（主要用于在web条件下，登录之后跳转至固定页面）
 * @param {Object} path
 * @param {Object} redirectUrl
 */
export function pathAddRedirectUrl(path, redirectUrl) {
	const { search, searchQuery, hashQuery } = uni.$xeu.parseUrl(location.href)
	if (uni.$xeu.isEmpty(redirectUrl) || redirectUrl == config.NO_TOKEN_BACK_URL || search.indexOf('redirect=')  != -1 ) {
		return path += `${search}`
	}
	path += `?redirect=${redirectUrl}`
	if (!uni.$xeu.isEmpty(searchQuery)) {
		path += `&${uni.$xeu.serialize(searchQuery)}`
	}
	if (!uni.$xeu.isEmpty(hashQuery)) {
		path += `&${uni.$xeu.serialize(hashQuery)}`
	}
	return path
}

/**
 * h5条件下通过url获取路由地址
 */
export function getH5RouteByUrl(){
	const { hashKey, pathname } = uni.$xeu.parseUrl(location.href)
	if (!uni.$xeu.isEmpty(hashKey)) {
		return hashKey
	}
	if (manifest.h5.router.base.length > 0) {
		return pathname.substring(manifest.h5.router.base.length - 1)
	} else {
		return pathname
	}
	
}
// #endif

